package com.zdgk.core.web.controller;

import com.zdgk.core.control.config.ControlConfigInfo;
import com.zdgk.core.control.config.MqttConfigInfo;
import com.zdgk.core.control.forward.modbus.ModbusSlaveConfig;
import com.zdgk.core.control.forward.network.IEC104Config;
import com.zdgk.core.control.read.modbus.ModbusMasterConfig;
import com.zdgk.core.mqtt.MqttClientConfig;
import com.zdgk.core.web.common.CommonResult;
import com.zdgk.core.web.entity.ConfigInfo;
import com.zdgk.core.web.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/config")
public class ConfigController {
    @Autowired
    private ConfigService configService;
    @Autowired
    private MqttClientConfig mqttClient;
    @Autowired
    private ModbusMasterConfig modbusMasterConfig;
    @Autowired
    private IEC104Config iec104Config;
    @Autowired
    private ModbusSlaveConfig modbusSlaveConfig;

    @GetMapping("/all")
    public CommonResult<List<ConfigInfo>> listAll() {
        return CommonResult.data(configService.getAllConfigs());
    }

    @GetMapping("/get/key/{key}")
    public CommonResult<ConfigInfo> getByKey(@PathVariable String key) {
        return CommonResult.data(configService.getByKey(key));
    }
    @GetMapping("/get/control")
    public CommonResult<ControlConfigInfo> getControlKey() {
        return CommonResult.data(configService.getControlKey());
    }
    @GetMapping("/get/mqtt")
    public CommonResult<MqttConfigInfo> getMqttKey() {
        return CommonResult.data(configService.getMqttKey());
    }

    @PostMapping("/set")
    public CommonResult<?> setKey(@RequestBody ConfigInfo configInfo) {
        configService.setKey(configInfo);
        return CommonResult.ok();
    }
    @PostMapping("/set/control")
    public CommonResult<?> setControlKey(@RequestBody ControlConfigInfo controlConfigInfo) {
        configService.setControlKey(controlConfigInfo);
        return CommonResult.ok();
    }
    @PostMapping("/set/matt")
    public CommonResult<?> setMqttKey(@RequestBody MqttConfigInfo mqttConfigInfo) {
        configService.setMqttKey(mqttConfigInfo);
        return CommonResult.ok();
    }

    @PostMapping("/restart")
    public CommonResult<?> restart() {
        mqttClient.init();
        modbusMasterConfig.init();
        iec104Config.init();
        modbusSlaveConfig.init();
        return CommonResult.ok();
    }
}
